/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidMaxContent;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidMaxContentListPlugin implements PluginConnector {
	private IDao bidMaxContentDao;
	private Logger logger = Logger.getLogger(BidMaxContentListPlugin.class);

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			//单独处理的特殊参数情况[暂无]
			//-------------------
			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by create_time desc ");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidMaxContentDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidMaxContentDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行Content列表查询失败 ", e);
			result.setMessage("Content列表查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByProperty", "", "", BidMaxContent.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidMaxContentAll"));//获取所有content列表
		if (Validator.isNull(all)) {
			getBidMaxContentParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织BidMaxContent信息查询条件
	private void getBidMaxContentParams(MethodParam param, ParamsVo vo) {
		BidMaxContent bidMaxContent = new BidMaxContent();
		bidMaxContent = (BidMaxContent) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidMaxContent);
		if (Validator.isNotNull(bidMaxContent.getObjId())) {
			param.setParams("objId", bidMaxContent.getObjId());
		}
		if (Validator.isNotNull(bidMaxContent.getObjType())) {
			param.setParams("objType", bidMaxContent.getObjType());
		}
		if (Validator.isNotNull(bidMaxContent.getInfoContent())) {
			param.setParams("infoContent", bidMaxContent.getInfoContent());
		}
		if (Validator.isNotNull(bidMaxContent.getContentType())) {
			param.setParams("contentType", bidMaxContent.getContentType());
		}
		if (Validator.isNotNull(bidMaxContent.getIfRead())) {
			param.setParams("ifRead", bidMaxContent.getIfRead());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare1())) {
			param.setParams("spare1", bidMaxContent.getSpare1());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare2())) {
			param.setParams("spare2", bidMaxContent.getSpare2());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare3())) {
			param.setParams("spare3", bidMaxContent.getSpare3());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare4())) {
			param.setParams("spare4", bidMaxContent.getSpare4());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare5())) {
			param.setParams("spare5", bidMaxContent.getSpare5());
		}
		if (Validator.isNotNull(bidMaxContent.getTitle())) {
			param.setParams("title", " like '%" + bidMaxContent.getTitle() + "%' ");
		}
	}

	public void setBidMaxContentDao(IDao bidMaxContentDao) {
		this.bidMaxContentDao = bidMaxContentDao;
	}

}
